You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

[customId].js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { Grid, Typography } from '@mui/material';
  2. import { Box, Container } from '@mui/system';
  3. import { dehydrate, QueryClient } from '@tanstack/react-query';
  4. import Image from 'next/image';
  5. import { useRouter } from 'next/router';
  6. import React from 'react';
  7. import GridItem from '../../components/grid-item/GridItem';
  8. import Loader from '../../components/loader/Loader';
  9. import ProductCard from '../../components/product-card/ProductCard';
  10. import TabContent from '../../components/tab-content/TabContent';
  11. import { useFetchSingleProduct } from '../../hooks/useFetchProductData';
  12. import { getProductData } from '../../requests/products/producDataRequest';
  13. import { useStore, useStoreUpdate } from '../../store/cart-context';
  14. const SingleProduct = () => {
  15. const { addCartValue } = useStoreUpdate();
  16. const { cartStorage } = useStore();
  17. const router = useRouter();
  18. const { customId } = router.query;
  19. const { data, isLoading } = useFetchSingleProduct(customId);
  20. const addProductToCart = (quantity) => addCartValue(data.product, quantity);
  21. const inCart = cartStorage?.some(
  22. (item) => item.product.customID === data?.product.customID
  23. )
  24. ? true
  25. : false;
  26. if (isLoading) {
  27. return <Loader loading={isLoading} />;
  28. }
  29. return (
  30. <Box
  31. sx={{
  32. display: 'flex',
  33. flexDirection: 'column',
  34. }}
  35. >
  36. <Container>
  37. <Typography
  38. fontFamily={'body1.fontFamily'}
  39. fontSize="32px"
  40. sx={{ mt: 25, height: '100%', color: 'primary.main' }}
  41. >
  42. {data.product.name}
  43. </Typography>
  44. <Grid container spacing={2}>
  45. <Grid sx={{ display: 'flex' }} item md={6} sm={12}>
  46. <Image
  47. src={data.product.image}
  48. alt="product"
  49. width={900}
  50. height={700}
  51. />
  52. </Grid>
  53. <TabContent
  54. description={data?.product.description}
  55. inCart={inCart}
  56. price={data?.product.price}
  57. category={data?.product.category}
  58. addProductToCart={addProductToCart}
  59. />
  60. </Grid>
  61. <Typography
  62. sx={{
  63. mt: { xs: '60px', md: '100px', lg: '150px' },
  64. mb: 5,
  65. color: 'primary.main',
  66. fontSize: '32px',
  67. }}
  68. >
  69. Similar Products You May Like
  70. </Typography>
  71. <Grid container spacing={2}>
  72. {data.similarProducts.map((product) => (
  73. <GridItem key={product._id}>
  74. <ProductCard product={product} />
  75. </GridItem>
  76. ))}
  77. </Grid>
  78. </Container>
  79. </Box>
  80. );
  81. };
  82. export const getServerSideProps = async (context) => {
  83. const { params } = context;
  84. const { customId } = params;
  85. const queryClient = new QueryClient();
  86. await queryClient.prefetchQuery(
  87. ['product', customId],
  88. async () => await getProductData(customId)
  89. );
  90. return {
  91. props: {
  92. dehydratatedState: dehydrate(queryClient),
  93. },
  94. };
  95. };
  96. export default SingleProduct;